package action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import service.service_admin.Service_Admin;
import service.service_public.Service_Public;
import service.service_search.Service_Search;
import tools.Convert;
import tools.InterfaceStationPlaList;
import tools.PageBeanPlus;
import view.ViewOc_Station_PLA;
import entitybean.l_sequence.EntityBeanL_Sequence;
import entitybean.oc_boat_ice.EntityBeanOc_Boat_Ice;
import entitybean.oc_boat_pla.EntityBeanOc_Boat_Pla;
import entitybean.oc_depthlist_pla.EntityBeanOc_Depthlist_Pla;
import entitybean.oc_property_pla.EntityBean_Oc_Property_Pla;
import entitybean.oc_property_tools_pla.EntityBean_Oc_Property_Tools_Pla;
import entitybean.oc_sea_area_ice.EntityBeanOc_Sea_Area_Ice;
import entitybean.oc_sea_area_pla.EntityBeanOc_Sea_Area_Pla;
import entitybean.oc_station_ice.EntityBeanOc_Station_Ice;
import entitybean.oc_station_pla.EntityBeanOc_Station_Pla;
import entitybean.oc_voyage_ice.EnitityBeanOc_Voyage_Ice;
import entitybean.oc_voyage_pla.EnitityBeanOc_Voyage_Pla;
import form.StationPlaForm;

public class Action_Oc_Station_Pla extends DispatchAction{
	private Service_Admin service_admin;
    private Service_Public service_public;
 	private Service_Search service_search;
	public Service_Admin getService_admin() {
		return service_admin;
	}

	public void setService_admin(Service_Admin service_admin) {
		this.service_admin = service_admin;
	}

	public Service_Public getService_public() {
		return service_public;
	}

	public void setService_public(Service_Public service_public) {
		this.service_public = service_public;
	}

	public Service_Search getService_search() {
		return service_search;
	}

	public void setService_search(Service_Search service_search) {
		this.service_search = service_search;
	}

	public ActionForward admin_init_oc_station_pla(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		StationPlaForm actionForm = (StationPlaForm) form;
		actionForm.setCurrentpage("1");
		actionForm.setStation_name("");
		actionForm.setId("");
		actionForm.setOc_voyage_id(0);
		actionForm.setOver_time("");
		actionForm.setStart_time("");
		String sql = "select ID,STATION_NAME,LONGITUDE,DIMENSIONALITY,TERRITORIAL_WATERS,WORK_TIME,DEPTH,OC_VOYAGE_ID,"
			+ "(SELECT OC_SEA_AREA from OC_SEA_AREA_PLA where id=TERRITORIAL_WATERS)OC_SEA_AREA,"
			+ "(SELECT VOYAGE_NAME FROM OC_VOYAGE_PLA WHERE ID=OC_VOYAGE_ID)VOYAGE_NAME from OC_STATION_PLA where DEL='OFF' ORDER BY id DESC";
		PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
				Integer.valueOf(actionForm.getRowcountperpage()), Integer
						.valueOf(actionForm.getCurrentpage()),
				"loadrichtext()");
		InterfaceStationPlaList interfaceforresultsettolist = new InterfaceStationPlaList();
		List<ViewOc_Station_PLA> list = interfaceforresultsettolist.ViewOc_News(pagebeanplus.getFormatqueryresultset());

		request.setAttribute("pagebeanplus", pagebeanplus);
		request.setAttribute("list", list);
		request.setAttribute("stationform", actionForm);
		System.out.println("list.size()" + list.size());
		return mapping.findForward("success");
	}

	public ActionForward admin_load_oc_station_pla(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return mapping.findForward("success");
	}

	public ActionForward admin_add_oc_station_pla(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<EntityBeanOc_Sea_Area_Pla> list_sea_area=service_search.Find_EntityBeanOc_Sea_Area_Pla();
		List<EntityBeanOc_Boat_Pla> list_boat=service_search.Find_EntityBeanOc_Boat_Pla();

        request.setAttribute("list_sea_area", list_sea_area);
        request.setAttribute("list_boat", list_boat);
		return mapping.findForward("success");
	}

	public ActionForward admin_insert_oc_station_pla(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		StationPlaForm actionForm = (StationPlaForm) form;
		String ctd=actionForm.getCtd();
		String longitude=actionForm.getLongitude();
		String station_name=actionForm.getStation_name();
		Double depth = actionForm.getDepth();
		Integer oc_voyage_id=actionForm.getOc_voyage_id();
		String dimensionality=actionForm.getDimensionality();
		String work_time=actionForm.getWork_time();
		Integer territorial_waters=actionForm.getTerritorial_waters();
		EntityBeanOc_Station_Pla link=new EntityBeanOc_Station_Pla();
        link.setCtd(ctd);
        link.setDepth(Double.valueOf(depth));
        link.setDimensionality(dimensionality);
        link.setLongitude(longitude);
        link.setOc_voyage_id(oc_voyage_id);
        link.setStation_name(station_name);
        link.setTerritorial_waters(territorial_waters);
        link.setWork_time(new Date());
		link.setDel("OFF");
		work_time=work_time+" 00:00:00";
		Convert convert=new Convert();
		link.setWork_time(convert.ConvertStringToDate(work_time));
		Integer i=0;
		EntityBeanL_Sequence entity=new EntityBeanL_Sequence();
		entity=this.service_public.Find_L_Sequence_By_seq_Name("SEQ_OC_STATION_PLA");
		i=entity.getSeq_Value()+1;
		entity.setSeq_Value(i);
		link.setId(i);
		this.service_public.Update_L_Sequence(entity);
		this.service_search.Insert_Oc_Station_Pla(link);
		System.out.println("初始化新闻模块");
		// 封装新闻查询的数据及表单
		StationPlaForm goodForm = (StationPlaForm) form;
		goodForm.setCurrentpage("1");
		goodForm.setCtd("");
		goodForm.setStation_name("");
		goodForm.setOver_time("");
		goodForm.setStart_time("");
		String sql = "select ID,STATION_NAME,LONGITUDE,DIMENSIONALITY,TERRITORIAL_WATERS,WORK_TIME,DEPTH,OC_VOYAGE_ID,"
			+ "(SELECT OC_SEA_AREA from OC_SEA_AREA_PLA where id=TERRITORIAL_WATERS)OC_SEA_AREA,"
			+ "(SELECT VOYAGE_NAME FROM OC_VOYAGE_PLA WHERE ID=OC_VOYAGE_ID)VOYAGE_NAME from OC_STATION_PLA where DEL='OFF' ORDER BY id DESC";
		PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
				Integer.valueOf(goodForm.getRowcountperpage()), Integer
						.valueOf(goodForm.getCurrentpage()),
				"loadrichtext()");
		InterfaceStationPlaList interfaceforresultsettolist = new InterfaceStationPlaList();
		List<ViewOc_Station_PLA> list = interfaceforresultsettolist.ViewOc_News(pagebeanplus.getFormatqueryresultset());

		request.setAttribute("pagebeanplus", pagebeanplus);
		request.setAttribute("list", list);
		request.setAttribute("stationform", goodForm);
		return mapping.findForward("success");
	}

	public ActionForward admin_delete_station_pla(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		StationPlaForm actionForm = (StationPlaForm) form;
		String id = actionForm.getId();
		id = this.cut_tail(id);
		this.service_search.Delete_Oc_Station_Pla(id);

		String sql = "select ID,STATION_NAME,LONGITUDE,DIMENSIONALITY,TERRITORIAL_WATERS,WORK_TIME,DEPTH,OC_VOYAGE_ID,"
			+ "(SELECT OC_SEA_AREA from OC_SEA_AREA_PLA where id=TERRITORIAL_WATERS)OC_SEA_AREA,"
			+ "(SELECT VOYAGE_NAME FROM OC_VOYAGE_PLA WHERE ID=OC_VOYAGE_ID)VOYAGE_NAME from OC_STATION_PLA where DEL='OFF' ORDER BY id DESC";
		PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
				Integer.valueOf(actionForm.getRowcountperpage()), Integer
						.valueOf(actionForm.getCurrentpage()),
				"loadrichtext()");
		InterfaceStationPlaList interfaceforresultsettolist = new InterfaceStationPlaList();
		List<ViewOc_Station_PLA> list = interfaceforresultsettolist.ViewOc_News(pagebeanplus.getFormatqueryresultset());

		request.setAttribute("pagebeanplus", pagebeanplus);
		request.setAttribute("list", list);
		request.setAttribute("stationform", actionForm);
		return mapping.findForward("success");
	}
	public ActionForward admin_update_init_oc_station_pla(
			ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		StationPlaForm actionForm = (StationPlaForm) form;
		String id = actionForm.getId();
		List<EntityBeanOc_Sea_Area_Pla> list_sea_area = this.service_search.Find_EntityBeanOc_Sea_Area_Pla();
		List<EntityBeanOc_Boat_Pla> list_boat = this.service_search.Find_EntityBeanOc_Boat_Pla();

		EntityBeanOc_Station_Pla e = new EntityBeanOc_Station_Pla();
		e = this.service_search.Find_EntityBeanOc_Station_Pla(Integer
				.valueOf(id));

		String ns = "";
		String ew = "";
		String a[] = e.getLongitude().split(" ");
		String b[] = e.getDimensionality().split(" ");
		ew = a[0];
		ns = b[0];
		e.setLongitude(a[1]);
		e.setDimensionality(b[1]);
		actionForm.setEw(ew);
		actionForm.setNs(ns);
		// 配置相应的数据航次数据
		// e.getOc_voyage_id();//反查航次所属的科考船
		EnitityBeanOc_Voyage_Pla voy = this.service_search
				.Find_EnitityBeanOc_Voyage_Pla(e.getOc_voyage_id());
		EntityBeanOc_Boat_Pla boat = this.service_search
				.Find_EntityBeanOc_Boat_Pla(voy.getBoatid());
		List<EnitityBeanOc_Voyage_Pla> voyage_list = this.service_search
				.Find_Oc_Voyage_Pla_By_boatid(boat.getId());

		request.setAttribute("boat", boat);
		request.setAttribute("voyage_list", voyage_list);

		request.setAttribute("e", e);
		request.setAttribute("list_sea_area", list_sea_area);
		request.setAttribute("list_boat", list_boat);
		request.setAttribute("stationform", actionForm);
		return mapping.findForward("success");
	}
	public ActionForward admin_update_oc_station_pla(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		StationPlaForm actionForm = (StationPlaForm) form;
		String id=actionForm.getId();
		String ctd=actionForm.getCtd();
		String longitude=actionForm.getLongitude();
		String station_name=actionForm.getStation_name();
		Double depth = actionForm.getDepth();
		Integer oc_voyage_id=actionForm.getOc_voyage_id();
		String work_time=actionForm.getWork_time();
		String dimensionality=actionForm.getDimensionality();
		Integer territorial_waters=actionForm.getTerritorial_waters();
		EntityBeanOc_Station_Pla link=new EntityBeanOc_Station_Pla();
        link.setCtd(ctd);
        link.setDepth(Double.valueOf(depth));
        link.setDimensionality(dimensionality);
        link.setLongitude(longitude);
        link.setOc_voyage_id(oc_voyage_id);
        link.setStation_name(station_name);
        link.setTerritorial_waters(territorial_waters);
        link.setWork_time(new Date());
		link.setDel("OFF");
		work_time=work_time+" 00:00:00";
		Convert convert=new Convert();
		link.setWork_time(convert.ConvertStringToDate(work_time));
		link.setId(Integer.valueOf(id));
		this.service_search.Update_Oc_Station_Pla(link);
		StationPlaForm goodForm = (StationPlaForm) form;
		goodForm.setCurrentpage("1");
		goodForm.setOver_time("");
		goodForm.setStart_time("");
		String sql = "select ID,STATION_NAME,LONGITUDE,DIMENSIONALITY,TERRITORIAL_WATERS,WORK_TIME,DEPTH,OC_VOYAGE_ID,"
			+ "(SELECT OC_SEA_AREA from OC_SEA_AREA_PLA where id=TERRITORIAL_WATERS)OC_SEA_AREA,"
			+ "(SELECT VOYAGE_NAME FROM OC_VOYAGE_PLA WHERE ID=OC_VOYAGE_ID)VOYAGE_NAME from OC_STATION_PLA where DEL='OFF' ORDER BY id DESC";
		PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
				Integer.valueOf(goodForm.getRowcountperpage()), Integer
						.valueOf(goodForm.getCurrentpage()),
				"loadrichtext()");
		InterfaceStationPlaList interfaceforresultsettolist = new InterfaceStationPlaList();
		List<ViewOc_Station_PLA> list = interfaceforresultsettolist.ViewOc_News(pagebeanplus.getFormatqueryresultset());

		request.setAttribute("pagebeanplus", pagebeanplus);
		request.setAttribute("list", list);
		request.setAttribute("stationform", goodForm);
	    return mapping.findForward("success");
	}

	public ActionForward admin_search_oc_station_pla(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		StationPlaForm actionForm = (StationPlaForm) form;
		String sql = "select ID,STATION_NAME,LONGITUDE,DIMENSIONALITY,TERRITORIAL_WATERS,WORK_TIME,DEPTH,OC_VOYAGE_ID,"
			+ "(SELECT OC_SEA_AREA from OC_SEA_AREA_PLA where id=TERRITORIAL_WATERS)OC_SEA_AREA,"
			+ "(SELECT VOYAGE_NAME FROM OC_VOYAGE_PLA WHERE ID=OC_VOYAGE_ID)VOYAGE_NAME from OC_STATION_PLA where DEL='OFF' ORDER BY id DESC";
		PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
				Integer.valueOf(actionForm.getRowcountperpage()), Integer
						.valueOf(actionForm.getCurrentpage()),
				"loadrichtext()");
		InterfaceStationPlaList interfaceforresultsettolist = new InterfaceStationPlaList();
		List<ViewOc_Station_PLA> list = interfaceforresultsettolist.ViewOc_News(pagebeanplus.getFormatqueryresultset());

		request.setAttribute("pagebeanplus", pagebeanplus);
		request.setAttribute("list", list);
		request.setAttribute("stationform", actionForm);
		return mapping.findForward("success");
	}

	private String cut_tail(String str) {
		String cut = str;
		if (str.endsWith(",")) {
			cut = cut.substring(0, cut.length() - 1);
		}
		return cut;
	}
}
